package com.devtty.collabhr.ui;

//import com.devtty.About;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.PopupView.PopupVisibilityEvent;
import com.vaadin.ui.UriFragmentUtility;
import com.vaadin.ui.UriFragmentUtility.FragmentChangedEvent;
import com.vaadin.ui.UriFragmentUtility.FragmentChangedListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.themes.Reindeer;

public class MainWindow extends Window {

	BreadCrumbs breadcrumbs = new BreadCrumbs();
	UriFragmentUtility uriFragmentUtility = new UriFragmentUtility();
	
	private final HorizontalSplitPanel mainSplit;
	
	private ComboBox search;
	private Button callButton;
	
	public MainWindow(){
		VerticalLayout mainExpand = new VerticalLayout();
		setContent(mainExpand);
		setSizeFull();
		mainExpand.setSizeFull();
		//setTheme("sampler-reindeer");
		
		//topbar
		HorizontalLayout nav = new HorizontalLayout();
		mainExpand.addComponent(nav);
		nav.setHeight("44px");
		nav.setWidth("100%");
		nav.setStyleName(Reindeer.LAYOUT_BLACK);
		nav.setStyleName("topbar");
		nav.setSpacing(true);
		nav.setMargin(false, true, false, false);
		
		
		//Upper left logo
		Component logo = createLogo();
		nav.addComponent(logo);
		nav.setComponentAlignment(logo, Alignment.MIDDLE_LEFT);
		
		//Breadcrumbs
		nav.addComponent(breadcrumbs);
		nav.setExpandRatio(breadcrumbs, 1);
		nav.setComponentAlignment(breadcrumbs, Alignment.MIDDLE_LEFT);
		
		//backbutton
		nav.addComponent(uriFragmentUtility);
		uriFragmentUtility.addListener(new FragmentChangedListener(){
			public void fragmentChanged(FragmentChangedEvent source){
				String frag = source.getUriFragmentUtility().getFragment();
				if(frag != null && frag.contains("/")){
					String[] parts = frag.split("/");
					frag = parts[parts.length - 1];
				}
				//setFeature(frag);
			}
		});
		
		//menu
		mainExpand.addComponent(createMenu());
		
		//main split
		mainSplit = new HorizontalSplitPanel();
		mainSplit.setSizeFull();
		mainSplit.setStyleName("main-split");
		mainExpand.addComponent(mainSplit);
		mainExpand.setExpandRatio(mainSplit, 1);
		mainSplit.addComponent(new CompanyTreeLayout());
		mainSplit.setSplitPosition(25);
		
		//layout topareabuttons
		HorizontalLayout quicknav = new HorizontalLayout();
		HorizontalLayout toolbtn = new HorizontalLayout();
		nav.addComponent(toolbtn);
		nav.addComponent(quicknav);
		
		nav.setComponentAlignment(quicknav, Alignment.MIDDLE_LEFT);
		nav.setComponentAlignment(toolbtn, Alignment.MIDDLE_LEFT);
		quicknav.setStyleName("segment");
		toolbtn.setStyleName("segment");
		
		callButton = createCallButton();
		toolbtn.addComponent(callButton);
		
		//Search combobox
		Component searchComponent = createSearch();
		quicknav.addComponent(searchComponent);
	}
	
	private Component createSearch(){
		search = new ComboBox();
		search.setWidth("160px");
		search.setNewItemsAllowed(false);
		search.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
		search.setNullSelectionAllowed(true);
		search.setImmediate(true);
		search.setInputPrompt("Search samples ...");
		
		final PopupView pv = new PopupView("<span>Search</span>", search);
		pv.addListener(new PopupView.PopupVisibilityListener() {
			public void popupVisibilityChange(PopupVisibilityEvent event) {
				if(event.isPopupVisible()){
					search.focus();
				}
			}
		});
		pv.setStyleName("quickjump");
		pv.setDescription("Quick jump");
		return pv;
	}
	
	private Component createLogo(){
		Button logo = new NativeButton("", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				//TODO not impl
			}
		});
		logo.setDescription("Home");
		logo.setStyleName(BaseTheme.BUTTON_LINK);
		logo.addStyleName("logo");
		return logo;
	}
	
	private Component createMenu(){
		MenuBar menuBar = new MenuBar();
		menuBar.setWidth("100%");
		
		final MenuBar.MenuItem file = menuBar.addItem("File", null);
		final MenuBar.MenuItem help = menuBar.addItem("Help", null);
		
        final MenuBar.MenuItem newItem = file.addItem("New", null);
        file.addItem("Open file..." , null);
        file.addSeparator();
        
        newItem.addItem("Employee", null);
        newItem.addItem("Project", null);
        newItem.addItem("Customer", null);
        newItem.addItem("Leave Request", null);
        newItem.addItem("Timesheet", null);
        newItem.addItem("Vacancy", null);
        
        
        file.addItem("Close", null);
        
        final AboutWindow a = new AboutWindow();
        
        help.addItem("Help", null);
        help.addSeparator();
        help.addItem("About", new Command(){
        	public void menuSelected(MenuItem selectedItem){
        		getApplication().getMainWindow().addWindow(a);
        	}
        });
        
        
        return menuBar;
	}


	private Button createCallButton(){
		Button b = new NativeButton("Anruf");
		final QuickCallWindow w = new QuickCallWindow();
		b.addListener(new ClickListener(){

			public void buttonClick(ClickEvent event) {
				getApplication().getMainWindow().addWindow(w);
			}
			
		});
		return b;
	}
	
	private class BreadCrumbs extends CustomComponent{
		HorizontalLayout layout;
		
		BreadCrumbs(){
			layout = new HorizontalLayout();
			layout.setSpacing(true);
			setCompositionRoot(layout);
			setStyleName("breadcrumbs");
		}
	}

}
